home *** CD-ROM | disk | FTP | other *** search
- Path: uni-erlangen.de!winx03!sunshine!schoof
- From: schoof@informatik.uni-wuerzburg.de (Jochen Schoof)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with c code, please help!
- Date: 19 Jan 1996 11:52:27 GMT
- Organization: University of Wuerzburg, Germany
- Message-ID: <4do0lr$f5n@winx03.informatik.uni-wuerzburg.de>
- References: <surgsw-1901960148530001@128.206.206.86>
- NNTP-Posting-Host: wi2x01.informatik.uni-wuerzburg.de
- X-Newsreader: TIN [version 1.2 PL2]
-
- Joel Weinstein (surgsw@mizzou1.missouri.edu) wrote:
- : I have been trying to get this very simple piece of code to work for
- : hours. What is the problem???????
-
- : #include <stdio.h>
-
- : main()
- : {
- : int i=0;
- : char word[100], c;
- : printf("Enter a word: ");
- : while( (c = getchar()) != '\n') {
- : *word = c;
- : word == word + 1;
- : }
- : printf("You entered: ");
- : *word = 0;
- : while( *word != '\n' )
- : {
- : putchar( *word );
- : word == word + 1;
- : }
- : }
-
- : I think the problem I am having is due to my not knowing when to use the
- : '*' operator. When do you use it? Also, why won't my goddam compiler let
- : me do word++; instead of word == word +1; I also tried to do *word++;
- : and it didn't work, what is the deal with that. It won't let me put word
- : = word + 1;. It insists on the double ='s. Why is that?
-
- word is the base of an array you defined and therefore cannot be changed,
- because you would lose the array in that case. What you need is a special
- pointer to move through your char array. word == word + 1; works but does
- not do what you think it does. It only compares word to word+1, which is
- always false, and throws away this result. The compiler did definetly not
- insist on the comparison, but it was your idea, because you did not get
- some of C's basic concepts :-)
- Using the * is relatively simple: Whenever you access the object a pointer
- points to, use * to dereference it. Changing the pointer itself, e.g. when
- moving through an array you need no *. In your program *word is completely
- equivalent to word[0] and with C not letting you change the value of word,
- it will never mean something different. With my additions wp is a pointer
- to char and *wp is a char variable, but not a specific one. I hope you got
- the idea. Not being a native speaker it's not too easy to explain it.
-
- Here is a solution with minimal changes:
-
- #include <stdio.h>
-
- int main(void)
- {
- char word[100], *wp=word, c; /* I added wp as described above */
-
- printf("Enter a word: ");
- while( (c = getchar()) != '\n') {
- *wp = c;
- wp++;
- }
-
- /* Now you've read the newline character, but haven't copied it */
-
- *wp='\n'; /* now the newline is copied to the array */
- printf("You entered: ");
- wp = word; /* re-initialize the pointer*/
- while( *wp != '\n' )
- {
- putchar( *wp );
- wp++;
- }
- return 0;
- }
-
-
- Let me add, that different techniques should be used for reading and
- writing words. It might be a good idea to take a look at the functions
- scanf() and printf() for this purpose. Even when working with strings
- C offer nice functions in its standard library.
-
- : ps: is there a way to find out what exactly error messages mean? I kept
- : getting something similar to: not an Ivalue. It would be nice if I knew
- : what the hell that meant.
-
- A good reference manual should explain its diagnostics. If it doesn't
- I'd get another compiler.
-
- - Jochen
-
- --
- --------------------------------------------------------------------------
- Jochen Schoof mailto:schoof@informatik.uni-wuerzburg.de
- Lehrstuhl fuer Informatik II +-------------------------------------------
- Universitaet Wuerzburg | You are just reading a .sig-light:
- D-97074 Wuerzburg (Germany) | It is free of fat, sugar and cholesterol!
- ------------------------------+-------------------------------------------
- WWW-Homepage: http://www.informatik.uni-wuerzburg.de/staff/joscho
- --------------------------------------------------------------------------
-